package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysorg.dto.SysOrgDTO;
import com.talkweb.newproject.admin.sysorg.service.SysOrgService;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by qsf on 2019/5/7.
 */
@RestController
@RequestMapping(value="/api/sysOrg")
@Slf4j
public class SysOrgRest {
    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 新增组织信息
     * @param sysOrgDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method = RequestMethod.POST)
    public ResponseResult create(@RequestBody SysOrgDTO sysOrgDTO) throws Exception{
        ResponseResult result = new ResponseResult();
        try {
            sysOrgService.insertSysOrg(sysOrgDTO);
            result.setStatus(Status.SUCCESS);
            result.setMessage("保存成功!");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试!");
            return result;
        }
    }


    /**
     * 修改组织信息
     * @param sysOrgDTO
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method = RequestMethod.POST)
    public ResponseResult update(@RequestBody SysOrgDTO sysOrgDTO) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysOrgService.updateSysOrg(sysOrgDTO);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试");
            return result;
        }
    }


    /**
     * 删除组织信息
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteOrgById(@RequestParam("id") String id) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysOrgService.deleteOrgById(id);
            result.setStatus(Status.SUCCESS);
            result.setMessage("删除成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试");
            return result;
        }

    }

    /**
     * 根据主键查询组织信息
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
    public ResponseResult<SysOrgDTO> querySysOrgByPrimaryKey(@PathVariable("id") String id) throws Exception{
        ResponseResult result = new ResponseResult();
        try{
            SysOrgDTO sysOrgDTO = sysOrgService.querySysOrgDtoByPrimaryKey(id);
            result.setStatus(Status.SUCCESS);
            result.setData(sysOrgDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试");
            return result;
        }

    }

    /**
     * 根据条件查询组织信息
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysOrgDTO> queryLike(SysOrgDTO obj) throws Exception {
        ResponseResult<SysOrgDTO> result = new ResponseResult<>();
        try{
            SysOrgDTO sysOrgDTO = sysOrgService.queryLikeSysOrg(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(sysOrgDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试");
            return result;
        }

    }

    @ResponseBody
    @RequestMapping(value="/getSysOrgList",method = RequestMethod.POST)
    public ResponseResult<List<SysOrgDTO>> querySysOrgList(@RequestBody SysOrgDTO obj) throws Exception {
        ResponseResult<List<SysOrgDTO>> result = new ResponseResult<>();
        try{
            List<SysOrgDTO> sysOrgDTOList = sysOrgService.searchSysOrg(obj);
            result.setStatus(Status.SUCCESS);
            result.setData(sysOrgDTOList);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行失败,请重试");
            return result;
        }



    }
}
